package com.mooer.app;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.mooer.MooerApplication;
import com.mooer.R;
import com.mooer.bean.SearchLocation;
import com.mooer.bean.Shop;
import com.mooer.exception.SVException;
import com.mooer.http.HttpUtils;
import com.mooer.utils.ui.ProgressLinearLayout;
import com.mooer.utils.ui.UiUtils;

public class SignBusiness extends Activity implements OnClickListener {

	private EditText edit;

	private ImageView searchButton;

	private ListView listView;

	private SignBusinessAdapter adapter;

	private ProgressLinearLayout footer;

	private List<Shop> list = new ArrayList<Shop>();

	private MooerApplication mooerApp;

	private SearchLocation location;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sign_business);
		mooerApp = (MooerApplication) getApplication();
		edit = (EditText) findViewById(R.id.search);
		searchButton = (ImageView) findViewById(R.id.search_btn);
		listView = (ListView) findViewById(R.id.listview);
		listView = (ListView) findViewById(R.id.listview);
		footer = (ProgressLinearLayout) LayoutInflater.from(this).inflate(
				R.layout.progress_small, null);
		footer.initView();
		listView.addFooterView(footer);
		adapter = new SignBusinessAdapter();
		listView.setAdapter(adapter);

		searchButton.setOnClickListener(this);
		footer.setOnClickListener(this);

		location = new SearchLocation();
		location.setLatitude(32.05724);
		location.setLongitude(118.740275);
		// location.setLatitude(mooerApp.getLatitude());
		// location.setLongitude(mooerApp.getLongitude());
		location.setOrderBy("concern_num");
		location.setRadius(2000);
		location.setRegion(3);
		location.setCategory(67);
		new SignBusinessTask().execute(location);
	}

	public class SignBusinessAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			View itemView = (LinearLayout) LayoutInflater.from(
					SignBusiness.this).inflate(R.layout.sign_business_item,
					null);
			ImageView pic = (ImageView) itemView.findViewById(R.id.icon);
			TextView name = (TextView) itemView
					.findViewById(R.id.business_name);
			TextView address = (TextView) itemView
					.findViewById(R.id.business_address);
			TextView meters = (TextView) itemView
					.findViewById(R.id.business_meters);
			name.setText(list.get(position).getName());
			address.setText(list.get(position).getAddress().getAddress1());
			meters.setText(list.get(position).getDistance() + "米");
			pic.setImageBitmap(list.get(position).getIcon());
			itemView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Shop shop = list.get(position).copy();
					Intent intent = new Intent(SignBusiness.this,
							Business.class);
					intent.putExtra("businessInfo", shop);
					intent.putExtra("tab", "sign");
					startActivity(intent);
				}
			});
			return itemView;
		}

	}

	@Override
	public void onClick(View v) {
		if (v == searchButton) {
			if (edit.getText().toString().length() > 0) {

			} else {
				UiUtils.showToast(this, "请输入您要查询的关键字");
			}
		}

	}

	public class SignBusinessTask extends
			AsyncTask<SearchLocation, Integer, List<Shop>> {

		private boolean isException;

		@Override
		protected List<Shop> doInBackground(SearchLocation... location) {
			List<Shop> list = null;
			try {
				list = HttpUtils.fetchNearShopList(location[0], 0, 100);
				for (Shop shop : list) {
					shop.setIcon(mooerApp.getShopListDefaultIcon());
				}
			} catch (Exception e) {
				isException = true;
				e.printStackTrace();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<Shop> result) {
			super.onPostExecute(result);
			if (isException) {
				footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
						View.VISIBLE);
			} else {
				if (result.size() > 0) {
					list.addAll(result);
					listView.removeFooterView(footer);
					adapter.notifyDataSetChanged();
				} else {
					footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
							View.VISIBLE);
					footer.setText(null, "暂无可签到商家");
				}
			}
		}

	}

}
